package bdd.daos.impl;

import bdd.DBLink;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bdd.beans.*;
import bdd.daos.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Set;

public class DAOJoursImpl implements DAOJours {
	private DBLink dblink;

	public DAOJoursImpl() {		
		super();
		this.dblink = new DBLink(); 
		this.dblink.connexion();
	}

	@Override
	public Jours save(Jours o) {
		Jours retour = o;
		try {
			this.dblink.connexion();
			DateFormat format = new SimpleDateFormat("dd-MM-yy");
			String r = "INSERT INTO JOURS (IDDATE)" +
			" VALUES ('"+format.format(o.getIddate())+ "')";
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return retour;
	}

	@Override
	public Jours find(Date iddate) {
		Jours retour = new Jours();
		retour.setIddate(iddate);
		try {
			this.dblink.connexion();
			DateFormat format = new SimpleDateFormat("dd-MM-yy");
			String r = "SELECT * FROM JOURS WHERE IDDATE = '" + format.format(iddate)+"'";
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
                                retour.setIddate(rs.getDate("IDDATE"));
			}		
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te" + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return retour;
	}

	@Override
	public List<Jours> findAll() {
		List<Jours> Lstretour = new ArrayList();
		try {
			this.dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("SELECT * FROM JOURS"));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				Jours j = new Jours();
                                j.setIddate(rs.getDate("IDDATE"));
				Lstretour.add(j);
			}		
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te" + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return Lstretour;
	}

	@Override
	public void update(Jours o) {
		try {
			this.dblink.connexion();
			DateFormat format = new SimpleDateFormat("dd-MM-yy");
			String r = "UPDATE JOURS SET IDDATE='"+o.getIddate()+ "' WHERE IDDATE='"+format.format(o.getIddate())+"'";
			
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
	}

	@Override
	public void delete(Jours o) {
		try {
			this.dblink.connexion();
			String r = "DELETE FROM JOURS WHERE IDDATE="+o.getIddate();
			
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
	}

    public Set<Jours> findByPeriode(Integer numPeriode) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
